package com.pn.controller;

import com.pn.entity.Pet;
import com.pn.service.PetService;
import com.pn.service.PetTypeService;
import com.pn.util.MyJsonUtils;
import com.pn.util.PageCriterion;
import com.pn.util.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class PetController {

    @Autowired
    PetService petService;

    @Autowired
    PetTypeService petTypeService;

    /**
     * 返回分页查询到的不同种类的动物
     * @param models
     * @return
     * @throws Exception
     */
    @PostMapping("/findByCriterion")
    public List<Pet> findListPetsByCriterion(@RequestBody Map<String,Object> models) throws Exception {
        ResponseMsg responseMsg = new ResponseMsg();
        PageCriterion pageCriterion= MyJsonUtils.map2obj((Map<String,Object>)models.get("page"),PageCriterion.class);
        System.out.println(pageCriterion);
        Pet petInfoForSearch=MyJsonUtils.map2obj((Map<String,Object>)models.get("pet"),Pet.class);
        System.out.println(petInfoForSearch);
        List<Pet> petList = petService.listByPtidByPage(pageCriterion,petInfoForSearch.getPtid());
        System.out.println(petList);
        return petList;
    }

    /**
     * 根据pid返回查询到的宠物详细信息
     * @param pid
     * @return
     */
    @GetMapping("/getPetInfo")
    public Pet findDetail(@RequestParam Integer pid){
        System.out.println(pid);
        Pet getPet = petService.getById(pid);
        System.out.println(getPet);
        return getPet;
    }

    @GetMapping("/getPetType")
    public String findType(@RequestParam Integer ptid){
        return petTypeService.getType(ptid);
    }
}
